/**
 * 
 */
package com.yonyou.iuap.controller.app;

import java.util.Date;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yonyou.iuap.pub.IDFactory;
import com.yonyou.iuap.pub.JsonUtils;
import com.yonyou.iuap.shell.entity.KmCommentEntity;
import com.yonyou.iuap.shell.entity.KmKnowledgeStatisticsEntity;
import com.yonyou.iuap.shell.pub.ResultDto;
import com.yonyou.iuap.shell.service.intf.IKmCommentService;
import com.yonyou.iuap.shell.service.intf.IKnowledgeStatisticsService;

/**
 * @author ganchj
 * 评论增删改查
 */
@Controller
@RequestMapping("/comment")
public class KmCommentController {
	private final Logger logger = (Logger) LoggerFactory.getLogger(getClass());
	private long start,end;
	@Autowired
	private IKmCommentService iKmCommentService;
	@Autowired
	private IKnowledgeStatisticsService iknowledgeStatisticsService;
	//给知识点添加评论
	@RequestMapping(value="/app/addComment",method=RequestMethod.POST)
	public @ResponseBody String addComment(@RequestBody HashMap parMap,HttpServletRequest request,HttpServletResponse response) {
		start=System.currentTimeMillis();
		KmCommentEntity entity=new KmCommentEntity();
		//评论内容
		String comment_contant=parMap.get("comment_contant").toString();
		entity.setComment_contant(comment_contant);
		//评论人
		String pk_user=parMap.get("pk_user").toString();
		entity.setPk_user(pk_user);
		//评论知识点pk
		String pk_publish=parMap.get("pk_publish").toString();
		entity.setPk_publish(pk_publish);
		//知识点类型0表示对知识点的评论，1表示对评论的回复
		entity.setComment_type(0);
		//非空判断
		if(!StringUtils.isEmpty(comment_contant) && !StringUtils.isEmpty(comment_contant) && !StringUtils.isEmpty(comment_contant)){
			ResultDto resultDto = iKmCommentService.insert(entity);
			end=System.currentTimeMillis();
			logger.info("addComment接口调用时间"+String.valueOf(end-start));
			return JsonUtils.toJsonStr(resultDto);
		}else {
			ResultDto resultDto = new ResultDto();
			resultDto.setStatus(1);
			resultDto.setMessage("参数不能为空");
			end=System.currentTimeMillis();
			logger.info("addComment接口调用时间"+String.valueOf(end-start));
			return JsonUtils.toJsonStr(resultDto);
		}
		
	}
	
	//评论回复接口
	@RequestMapping(value="/app/answer",method=RequestMethod.POST)
	public @ResponseBody String answer(@RequestBody HashMap parMap,HttpServletRequest request,HttpServletResponse response) {
		start=System.currentTimeMillis();
		KmCommentEntity entity=new KmCommentEntity();
		//回复内容
		String comment_contant=parMap.get("comment_contant").toString();
		entity.setComment_contant(comment_contant);
		//回复人
		String pk_user=parMap.get("pk_user").toString();
		entity.setPk_user(pk_user);
		//回复所在知识点pk
		String pk_publish=parMap.get("pk_publish").toString();
		entity.setPk_publish(pk_publish);
		//所回复评论的pk
		String pk_parent_comment=parMap.get("pk_parent_comment").toString();
		entity.setPk_parent_comment(pk_parent_comment);
		//所回复评论的发布者
		String pk_parent_user=parMap.get("pk_parent_user").toString();
		entity.setPk_parent_user(pk_parent_user);
		//知识点类型0表示对知识点的评论，1表示对评论的回复
		entity.setComment_type(1);
		//非空判断
		if(!StringUtils.isEmpty(comment_contant) && !StringUtils.isEmpty(comment_contant) && !StringUtils.isEmpty(comment_contant)
				&& !StringUtils.isEmpty(pk_parent_comment)&& !StringUtils.isEmpty(pk_parent_user)){
			ResultDto resultDto = iKmCommentService.insert(entity);
			end=System.currentTimeMillis();
			logger.info("answer接口调用时间"+String.valueOf(end-start));
			return JsonUtils.toJsonStr(resultDto);
		}else {
			ResultDto resultDto = new ResultDto();
			resultDto.setStatus(1);
			resultDto.setMessage("参数不能为空");
			end=System.currentTimeMillis();
			logger.info("answer接口调用时间"+String.valueOf(end-start));
			return JsonUtils.toJsonStr(resultDto);
		}
	}
	
	
	//评论查询接口
	@RequestMapping(value="/app/queryComment",method=RequestMethod.POST)
	public @ResponseBody String queryComment(@RequestBody HashMap<String,Object> parMap,HttpServletRequest request,HttpServletResponse response) {
		start=System.currentTimeMillis();
		
		ResultDto resultDto = iKmCommentService.queryComment(parMap);
//		统计
		KmKnowledgeStatisticsEntity bean=new KmKnowledgeStatisticsEntity();
		bean.setPK_USER((String) parMap.get("pk_user"));
		bean.setPK_KNOWLEDGE((String) parMap.get("pk_publish"));
		bean.setPK_KNOWLEDGE_STATISTICS(IDFactory.getInstance().getID());
		bean.setOPEN_TIME(new Date());
		bean.setPLATFORM("app");
		iknowledgeStatisticsService.insertSelective(bean);
		end=System.currentTimeMillis();
		logger.info("queryComment接口调用时间"+String.valueOf(end-start));
		return JsonUtils.toJsonStr(resultDto);
	}
}
